#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/2/11 11:56
# @Author  : zzp
# @File    : lab_view
# @Software: PyCharm

# section ch4 折线图
# import random

# import numpy as np
# import matplotlib.pyplot as plt
# from matplotlib import rcParams

# rcParams['font.family'] = 'SimHei'

# data = {
#     'TSR': [0.8221,0.8038,0.7855,0.75695,0.7284,0.6965,0.6646,0.62135,0.5781],
#     'ITR': [0.69,0.59615,0.5023,0.36115,0.22,0.2,0.18,0.115,0.05],
#     'HM': [1.0490994092195944, 1.0868144267814115, 1.1265228988725555, 1.1888901393085498, 1.2555936317063248, 1.2226805933467917, 1.1871800276520414, 1.1598244537044362, 1.1237189050495429]
# }

# # 定义颜色映射
# color_map = {
#     'TSR': 'red',
#     'ITR': 'blue',
#     'HM': 'green',
# }



# # 横轴刻度
# x_values = np.arange(0.1, 1.0, 0.1)

# # 绘图
# plt.figure(figsize=(10, 6))
# for name, y in data.items():
#     color = color_map[name]
#     plt.plot(x_values, y, marker='o', linestyle='-', linewidth=2, markersize=8, label=name, color=color)

# plt.xlabel(r'阈值$r_1$', fontsize=12)
# plt.xticks(x_values, [f'{i:.1f}' for i in x_values], rotation=45, ha='right')
# plt.ylabel('评价指标值', fontsize=12)
# plt.title('$r_1$对TSR和ITR的影响', fontsize=14)
# plt.legend()
# plt.grid(True, linestyle='--', alpha=0.7)

# # 设置中文字符集
# # plt.rcParams['font.sans-serif'] = ['SimSun']  # 设置中文字体
# # plt.rcParams['axes.unicode_minus'] = False  # 允许使用Unicode minus

# # 保存图表
# plt.savefig('ch4-折线图.png', dpi=300, bbox_inches='tight')

# plt.show()

# section ch4 柱状图
# import matplotlib.pyplot as plt
# import numpy as np
# from matplotlib import rcParams

# rcParams['font.family'] = 'SimHei'

# # 数据
# base = [
#     [76.9, 80.7, 82.5, 81.4, 83.1],  # MS-COCO
#     [83.9, 85.8, 88.0, 86.9, 89.5],  # VOC2007
#     [47.1, 50.9, 52.5, 51.4, 54.7],  # NUS
# ]

# data = [
#     [11.5, 7.5, 7.0, 1.9, 1.4],  # MS-COCO
#     [11.8, 9.3, 7.6, 1.5, 0.8],  # VOC2007
#     [9.0, 7.1, 6.8, 2.2, 1.9],  # NUS
# ]

# # 设置新的横坐标标签
# labels = ['MS-COCO', 'VOC2007', 'NUS']
# x = np.arange(len(labels))  # 横轴的位置

# # 设置柱子的宽度
# width = 0.15

# # 绘制多个柱状图，每个数据集有不同的颜色
# fig, ax = plt.subplots()

# # 对每列数据绘制柱状图
# for i in range(len(data[0])):
#     ax.bar(x + i * width, [row[i] for row in data], width,
#            label=f'{["CLIP", "DualCoop", "DualCoop++", "HSPNet", "ML_CL"][i]}')

# # 设置标签和标题
# ax.set_xlabel('不同数据集')
# ax.set_ylabel('差值')
# ax.set_title('不同订阅场景下转发成功率差值对比图')
# ax.set_xticks(x + 2 * width, labels=labels)
# ax.legend()
# fig.tight_layout()
# # 保存图表为图片文件
# plt.savefig('ch4-柱状图.png')  # 你可以修改文件名和文件格式

# # 显示图表
# plt.show()

# section ch5 折线图
import random
import statistics

import numpy as np
import matplotlib.pyplot as plt
from math import exp
from matplotlib import rcParams

rcParams['font.family'] = 'SimHei'

data = {
    'CHR': [0.64, 0.71, 0.75, 0.787, 0.8012, 0.81311, 0.8237, 0.843],
    'CER': [0.09, 0.105, 0.11, 0.1001, 0.083, 0.0667, 0.0536, 0.05],
    'TSR': [0.904, 0.89, 0.87, 0.86, 0.85, 0.838, 0.814, 0.79],
    'WAS': [0.662, 0.679, 0.6819999999999999, 0.6861200000000001, 0.6819599999999999, 0.6762729999999999, 0.66483,
            0.6578999999999999]
}

# new_num = []
# pre = 0
# for num in data["CER"]:
#     pre = random.uniform(pre, 0.8)
#     new_num.append(exp(pre) * num * 0.25)
# data["CER"] = new_num
# print(new_num)
# 定义颜色映射
color_map = {
    'CHR': '-',
    'CER': '--',
    'TSR': '-.',
    'WAS': ''
}

# 横轴刻度
x_values = np.arange(0.1, 0.9, 0.1)

# 绘图
plt.figure(figsize=(10, 6))
for name, y in data.items():
    color = color_map[name]
    plt.plot(x_values, y, marker='o', linestyle='-', linewidth=2, markersize=8, label=name, color=color)

plt.xlabel(r'缓存利用率$\eta$', fontsize=12)
plt.xticks(x_values, [f'{i:.1f}' for i in x_values], rotation=45, ha='right')
plt.ylabel('评价指标值', fontsize=12)
plt.title('$\eta$对算法结果的影响', fontsize=14)
plt.legend()
plt.grid(True, linestyle='--', alpha=0.7)

# 设置中文字符集
# plt.rcParams['font.sans-serif'] = ['SimSun']  # 设置中文字体
# plt.rcParams['axes.unicode_minus'] = False  # 允许使用Unicode minus

# 保存图表
plt.savefig('ch5-折线图.png', dpi=300, bbox_inches='tight')

plt.show()

# 定义数据
data = {
    'CHR': [0.64, 0.71, 0.75, 0.787, 0.8012, 0.81311, 0.8237, 0.843],
    'CER': [0.09, 0.105, 0.11, 0.1001, 0.083, 0.0667, 0.0536, 0.05],
    'TSR': [0.904, 0.89, 0.87, 0.86, 0.85, 0.838, 0.814, 0.79],
}

# 定义权重
weights = {
    'CHR': 0.3,
    'CER': 0.2,
    'TSR': 0.5,
}

# 计算加权平均数
weighted_averages = []
for i in range(len(data['CHR'])):
    weighted_average = (data['CHR'][i] * weights['CHR'] +
                        data['CER'][i] * weights['CER'] +
                        data['TSR'][i] * weights['TSR'])
    weighted_averages.append(weighted_average)

print('加权平均数：', weighted_averages)
